package xddq

import (
	"fmt"
	"time"
	D "websocket_apn/define"
	"websocket_apn/log"
	"websocket_apn/proto"
)

// 处理弟子试练神游关卡
func (xd *XdDriver) CheckPupilTravelInfoData(info D.PupilTravelInfo) {
	log.Infof("弟子试练-神游第%d-%d天，事件类型%02X，事件[%s]%s", info.Level, info.Day, info.EventType, info.EventId, info.EventDesc)
	if info.Level > 0 && info.Level < 60 {
		xd.ResetTaskByID(proto.S_PUPIL_EXPLORE_TRAVEL_PASS_TODAY)
	} else if info.Level == 60 {
		xd.ResetTaskByID(proto.S_PUPIL_EXPLORE_TRAVEL_START)
	}
}
func (xd *XdDriver) PupilChooseSkill(skillId []string) {
	time.Sleep(time.Second)
	finalIdx := 0
	bestSkillIdx := 99
	for idx, v := range skillId {
		skillIdx := proto.GetPupilSkillIdxById(v)
		if skillIdx < bestSkillIdx {
			bestSkillIdx = skillIdx
			finalIdx = idx
		}
	}
	log.Infof("弟子试练-选择第%d个技能 %s", finalIdx+1, proto.GetPupilSkillNameById(skillId[finalIdx]))
	xd.Send(proto.Get_Cmd_Data(xd.userInfo.PlayerId, proto.S_PUPIL_EXPLORE_CHOOSE_SKILL, fmt.Sprintf("08 %s 10 %02X", xd.UserData.Activity[D.Activity_Pupil].ID, finalIdx)))

}

// 处理弟子试练冒险信息
func (xd *XdDriver) CheckPupilExploreInfoData(cmd int32, info D.PupilExploreInfo) {
	log.Infof("弟子试练-冒险第%d天，事件类型%02X，事件[%s]%s", info.Day, info.EventType, info.EventId, info.EventDesc)
	//选技能
	if len(info.SkillListId) > 0 {
		xd.PupilChooseSkill(info.SkillListId)
		if cmd != proto.S_PUPIL_EXPLORE_NEXT_DAY_RSP {
			xd.ResetTaskByID(proto.S_PUPIL_EXPLORE_NEXT_DAY)
			return
		}
	}
	switch info.EventType {
	case 0x00:
	case 0x01:
		log.Infof("弟子试练-战斗")
		xd.Send(proto.Get_Cmd_Data(xd.userInfo.PlayerId, proto.S_PUPIL_EXPLORE_BATTLE, fmt.Sprintf("08 %s", xd.UserData.Activity[D.Activity_Pupil].ID)))

	case 0x02:
		log.Infof("弟子试练-小憩")
		xd.Send(proto.Get_Cmd_Data(xd.userInfo.PlayerId, proto.S_PUPIL_EXPLORE_SPRING_WATER, fmt.Sprintf("08 %s", xd.UserData.Activity[D.Activity_Pupil].ID)))
	case 0x03:
		log.Infof("弟子试练-挖掘")
		xd.Send(proto.Get_Cmd_Data(xd.userInfo.PlayerId, proto.S_PUPIL_EXPLORE_ROUND_TURN_TABLE, fmt.Sprintf("08 %s", xd.UserData.Activity[D.Activity_Pupil].ID)))
	case 0x04:
		log.Infof("弟子试练-造化")
		xd.Send(proto.Get_Cmd_Data(xd.userInfo.PlayerId, proto.S_PUPIL_EXPLORE_REC_TURN_TABLE, fmt.Sprintf("08 %s 10 01", xd.UserData.Activity[D.Activity_Pupil].ID)))
		xd.Send(proto.Get_Cmd_Data(xd.userInfo.PlayerId, proto.S_PUPIL_EXPLORE_REC_TURN_TABLE, fmt.Sprintf("08 %s 10 01", xd.UserData.Activity[D.Activity_Pupil].ID)))
	case 0x05:
		log.Infof("弟子试练-挑战怪物")
		xd.Send(proto.Get_Cmd_Data(xd.userInfo.PlayerId, proto.S_PUPIL_EXPLORE_WANDER_MONSTER, fmt.Sprintf("08 %s 10 01", xd.UserData.Activity[D.Activity_Pupil].ID)))
	case 0x06:
		log.Infof("弟子试练-古井 献出生命-15%")
		//00 生命-15% 攻击-15%
		xd.Send(proto.Get_Cmd_Data(xd.userInfo.PlayerId, proto.S_PUPIL_EXPLORE_EXCHANGE, fmt.Sprintf("08 %s 10 01", xd.UserData.Activity[D.Activity_Pupil].ID)))
	case 0x07:
		log.Infof("弟子试练-助威牛魔王")
		xd.Send(proto.Get_Cmd_Data(xd.userInfo.PlayerId, proto.S_PUPIL_EXPLORE_WAGER, fmt.Sprintf("08 %s 10 01", xd.UserData.Activity[D.Activity_Pupil].ID)))
		xd.Send(proto.Get_Cmd_Data(xd.userInfo.PlayerId, proto.S_PUPIL_EXPLORE_WAGER, fmt.Sprintf("08 %s 10 00", xd.UserData.Activity[D.Activity_Pupil].ID)))
	case 0x08:
		log.Infof("弟子试练-查看技能")
		xd.Send(proto.Get_Cmd_Data(xd.userInfo.PlayerId, proto.S_PUPIL_EXPLORE_SKILL_SELECT, fmt.Sprintf("08 %s", xd.UserData.Activity[D.Activity_Pupil].ID)))
	case 0x09:
		log.Infof("弟子试练-探索")
		xd.Send(proto.Get_Cmd_Data(xd.userInfo.PlayerId, proto.S_PUPIL_EXPLORE_REWARD, fmt.Sprintf("08 %s", xd.UserData.Activity[D.Activity_Pupil].ID)))
	case 0x0A:
		log.Infof("弟子试练-幸运")
		xd.Send(proto.Get_Cmd_Data(xd.userInfo.PlayerId, proto.S_PUPIL_EXPLORE_TEXT, fmt.Sprintf("08 %s", xd.UserData.Activity[D.Activity_Pupil].ID)))
	case 0x0B:
		log.Infof("弟子试练-AA")
		xd.Send(proto.Get_Cmd_Data(xd.userInfo.PlayerId, proto.S_PUPIL_EXPLORE_ROUND_TURN_TABLE, fmt.Sprintf("08 %s", xd.UserData.Activity[D.Activity_Pupil].ID)))
	case 0x0C:
		log.Infof("弟子试练-AA")
		xd.Send(proto.Get_Cmd_Data(xd.userInfo.PlayerId, proto.S_PUPIL_EXPLORE_ROUND_TURN_TABLE, fmt.Sprintf("08 %s", xd.UserData.Activity[D.Activity_Pupil].ID)))
	case 0x0D:
		log.Infof("弟子试练-机缘 生命上限+30%")
		xd.Send(proto.Get_Cmd_Data(xd.userInfo.PlayerId, proto.S_PUPIL_EXPLORE_DESTINY, fmt.Sprintf("08 %s 10 DE F9 36", xd.UserData.Activity[D.Activity_Pupil].ID)))
	case 0x0E:
		log.Infof("弟子试练-AA")
		xd.Send(proto.Get_Cmd_Data(xd.userInfo.PlayerId, proto.S_PUPIL_EXPLORE_ROUND_TURN_TABLE, fmt.Sprintf("08 %s", xd.UserData.Activity[D.Activity_Pupil].ID)))
	case 0x0F:
		//log.Infof("弟子试练-选技能")
		//xd.Send(proto.Get_Cmd_Data(xd.userInfo.PlayerId, proto.S_PUPIL_EXPLORE_CHOOSE_SKILL, fmt.Sprintf("08 %s 10 02", xd.UserData.Activity[D.Activity_Pupil].ID)))
	}
	xd.ResetTaskByID(proto.S_PUPIL_EXPLORE_NEXT_DAY)
}

// 处理弟子试练冒险信息
func (xd *XdDriver) CheckPupilRandomInfoData(allInfo D.PupilRandomInfo) {
	log.Infof("弟子试练-随机弟子，剩余刷新次数%d", allInfo.Count)
	if len(allInfo.Info) == 0 {
		return
	}
	//没有橙色弟子则再刷新一次
	if allInfo.Count > 0 && len(allInfo.Info) >= 3 && allInfo.Info[0].Flag < 3 && allInfo.Info[1].Flag < 3 && allInfo.Info[2].Flag < 3 {
		log.Infof("弟子试练-随机弟子再次刷新")
		xd.Send(proto.Get_Cmd_Data(xd.userInfo.PlayerId, proto.S_PUPIL_EXPLORE_RANDOM, fmt.Sprintf("08 %s 10 00", xd.UserData.Activity[D.Activity_Pupil].ID)))
	}
	finalIdx := -1
	highestGrade := byte(0)
	//选则品级最高的弟子
	for idx, info := range allInfo.Info {
		if info.Flag == 3 {
			finalIdx = idx
			break
		}
		if info.Flag > highestGrade {
			finalIdx = idx
			highestGrade = info.Flag
		}
	}
	log.Infof("弟子试练-选择第%d个弟子, 品级%d,生命%d,攻击%d,防御%d", finalIdx+1, allInfo.Info[finalIdx].Flag, allInfo.Info[finalIdx].Life, allInfo.Info[finalIdx].Attack, allInfo.Info[finalIdx].Defence)
	xd.Send(proto.Get_Cmd_Data(xd.userInfo.PlayerId, proto.S_PUPIL_EXPLORE_START, fmt.Sprintf("08 %s 10 %02X", xd.UserData.Activity[D.Activity_Pupil].ID, finalIdx)))
	xd.Send(proto.Get_Cmd_Data(xd.userInfo.PlayerId, proto.S_PUPIL_EXPLORE_TRAVEL_PASS_TODAY, fmt.Sprintf("08 %s", xd.UserData.Activity[D.Activity_Pupil].ID)))
}
